﻿#ifndef NSSHARE_SYS_EVENT_H
#define NSSHARE_SYS_EVENT_H

#include "../base/base_object.h"
#include "../utils/disallow_copy_and_assign.h"
#include "waitable.h"

namespace nsshare
{
namespace sys
{
class Event : public base::BaseObject,
              public Waitable
{
public:
    Event();
    ~Event();

    //auto reset, initial = unsignal
    bool init();
    //initial = unsignal
    bool init(bool manual_reset);

    bool init(bool manual_reset, bool initial_state);

    void fini();

    bool Set();
    bool Reset();
    bool Wait(uint32_t timeout);
    bool WaitEx(uint32_t timeout, bool alertable);

    void * WaitableHandle() const;

private:
    DISALLOW_COPY_AND_ASSIGN(Event);
        
private:
    HANDLE handle_;
};

class NamedEvent : public base::BaseObject,
                   public Waitable
{
public:
    NamedEvent();
    ~NamedEvent();

    bool Create(bool manual_reset, bool initial_state, const char * name);
    bool Open(const char * name);
    void Close();
    bool Set();
    bool Reset();
    bool Wait(uint32_t timeout);
    bool WaitEx(uint32_t timeout, bool alertable);
    bool IsPremier();

    void * WaitableHandle() const;

private:
    DISALLOW_COPY_AND_ASSIGN(NamedEvent);
        
private:
    HANDLE handle_;
};

}
}

#endif